package com.kamistoat.meimeistore.order.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kamistoat.meimeistore.order.entity.OrderEntity;
import com.kamistoat.meimeistore.order.service.OrderItemService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kamistoat.meimeistore.order.entity.OrderItemEntity;
import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.R;


/**
 * 订单项信息
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 16:42:29
 */
@RestController
@RequestMapping("meimeistoreorder/orderitem")
public class OrderItemController {
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("meimeistoreorder:orderitem:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = orderItemService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("meimeistoreorder:orderitem:info")
    public R info(@PathVariable("id") Long id) {
        OrderItemEntity orderItem = orderItemService.getById(id);

        return R.ok().put("orderItem", orderItem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("meimeistoreorder:orderitem:save")
    public R save(@RequestBody OrderItemEntity orderItem) {
        orderItemService.save(orderItem);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("meimeistoreorder:orderitem:update")
    public R update(@RequestBody OrderItemEntity orderItem) {
        orderItemService.updateById(orderItem);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("meimeistoreorder:orderitem:delete")
    public R delete(@RequestBody Long[] ids) {
        orderItemService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 测试向RabbitMQ发送消息用的
     */
    @RequestMapping("/testSendToRabbit")
    public void sendMessage() {
        for (int i = 0; i < 5; i++) {
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setId((long) i);
            orderEntity.setOrderSn("ssssss");
            orderEntity.setBillContent("sdgeg");
            orderEntity.setCouponId(9L);
            rabbitTemplate.convertAndSend("spring.direct.exchange",
                    "spring.news",
                    orderEntity,
                    new CorrelationData(UUID.randomUUID().toString()));
        }
    }
}
